# ydsp_server.py
import requests
from fastmcp import FastMCP
from dotenv import load_dotenv
import os

load_dotenv()

# 初始化 MCP 服务器
mcp = FastMCP("YDSPServer")

ydsp_api_url = os.getenv("BASE_YDSP_API_URL")  
def fetch_text_from_api(url,params):
    try:
        response = requests.get(url,params)
        response.raise_for_status()  # 检查请求是否成功
        return response.text  # 解析 JSON 数据
    except requests.exceptions.RequestException as e:
        print(f"Error fetching JSON from API: {e}")
        return []
@mcp.tool()
async def query_xm_files(xmbh: int) -> str:
    """根据提供的项目编号参数获取项目附件列表并输出md格式文本."""
    params = {
        "xmbh":xmbh
    }
    url = f"{ydsp_api_url}/fetch_xm_files"
    data = fetch_text_from_api(url,params)
    return data

@mcp.tool()
async def query_xm_file_url(url: str) -> str:
    """根据提供的附件url解析附件内容并输出md格式文本."""
    params = {
        "pdf_url":url
    }
    url = f"{ydsp_api_url}/readPdfData"
    data = fetch_text_from_api(url,params)
    return data
if __name__ == "__main__":
    mcp.run(transport="sse")  # 使用sse输入输出运行 MCP 服务器

